package com.jmb.api.web.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmb.api.common.login.LoginUser;
import com.jmb.api.common.login.LoginUserHolder;
import com.jmb.api.common.result.Result;
import com.jmb.api.entity.LogInfoVo;
import com.jmb.api.entity.User;
import com.jmb.api.vo.user.UserInfoVO;
import com.jmb.api.vo.user.UserQueryVO;
import com.jmb.api.vo.user.UserVO;
import com.jmb.api.web.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "用户管理")
@RestController
@RequestMapping("/web/user")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 用户注册
     */
    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result registerUser(@Valid @RequestBody User user)  {
        userService.registerUser(user);
        return Result.ok();
    }

    /**
     * 用户信息
     * @param userId 用户id
     * @return 用户信息
     */

    @Operation(summary = "用户信息")
    @GetMapping("/getUser")
    public Result<UserVO> getUser(@RequestParam Long userId){
        User user = userService.getById(userId);
        log.info("用户信息：{}",user);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);
        return Result.ok(userVO);
    }

    /**
     * 用户新增和更新
     * @param user 用户对象
     */
    @Operation(summary = "用户新增和更新")
    @PostMapping("/saveOrUpdateUser")
    public Result saveOrUpdateUser(@Valid @RequestBody User user){
        userService.saveOrUpdateUser(user);
        return Result.ok();
    }

    // 删除用户
    @Operation(summary = "删除用户")
    @DeleteMapping("/deleteUser")
    public Result deleteUser(@RequestParam Long userId){
        if (userId == 1){
            return Result.fail("超级管理员不能删除");
        }
        userService.removeById(userId);
        return Result.ok();
    }

    // 重置密码
    @Operation(summary = "重置密码")
    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestParam Long userId){
        userService.resetPassword(userId);
        return Result.ok();
    }

    /**
     *
     */
    @Operation(summary = "用户列表")
    @PostMapping("/list")
    public Result<IPage<UserInfoVO>> pageUser(@RequestBody UserQueryVO queryVO){
        IPage<UserInfoVO> page = new Page<>(queryVO.getPageNum(),queryVO.getPageSize());
        IPage<UserInfoVO> list = userService.pageUserInfo(page, queryVO);
        return Result.ok(list);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/info")
    public Result info(){
        LogInfoVo loginUserInfo = userService.getLoginUserInfo();

        return Result.ok(loginUserInfo);
    }
    @Operation(summary = "退出登录")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest  request){
        request.getHeader("access-token");
        // redis删除token
        return Result.ok();
    }

}
